import { isEmpty } from "@/utils"

/**
 * Token
 */
export function useToken() {
    /**
     * 本地存储Token
     */
    const useLocalToken = createGlobalState(() => useStorage<null | string>("__token__", null))

    const setToken = (accessToken: string) => {
        const token = useLocalToken()
        token.value = accessToken
    }

    const clearToken = () => {
        const token = useLocalToken()
        token.value = null
    }

    const getAccessToken = () => {
        return useLocalToken().value
    }

    const hasToken = computed(() => !isEmpty(useLocalToken().value))

    return { setToken, clearToken, getAccessToken, hasToken }
}
